package com.xc58.highreactor.compress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPCompress {
	public byte[] compress(byte[] data) throws Exception{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(bos);
		gzip.write(data);
		gzip.finish();
		gzip.close();
		byte[] result = bos.toByteArray();
		bos.close();
		return result;
	}
	
	public byte[] decompress(byte[] data) throws Exception{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(data));  
	    int count;  
	    byte result[] = new byte[1024*10];  
	    while ((count = gis.read(result, 0, 1024*10)) != -1) {  
	    	bos.write(result, 0, count);  
	    }  
	    gis.close(); 
	    byte[] ret = bos.toByteArray();
	    bos.close();
		return ret;
	}
}
